/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package pl.droidsonroids.gif;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.lang.ref.WeakReference;

class InvalidationHandler extends EventHandler {
    static final int MSG_TYPE_INVALIDATION = -1;

	private final WeakReference<GifDrawable> mDrawableRef;

	InvalidationHandler(final GifDrawable gifDrawable) {
		super(EventRunner.getMainEventRunner());
		mDrawableRef = new WeakReference<>(gifDrawable);
	}

	@Override
	protected void processEvent(InnerEvent event) {
		final GifDrawable gifDrawable = mDrawableRef.get();
		if (gifDrawable == null) {
			return;
		}
		if (event.eventId == MSG_TYPE_INVALIDATION) {
			gifDrawable.invalidateSelf();
		} else {
			for (AnimationListener listener : gifDrawable.mListeners) {
				listener.onAnimationCompleted(event.eventId);
			}
		}
	}
}
